package cn.stylefeng.guns.modular.time.controller;

import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.rule.annotation.BizLog;
import cn.stylefeng.roses.kernel.rule.pojo.request.BaseRequest;
import cn.stylefeng.roses.kernel.rule.pojo.response.ResponseData;
import cn.stylefeng.roses.kernel.rule.pojo.response.SuccessResponseData;
import cn.stylefeng.roses.kernel.scanner.api.annotation.ApiResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.GetResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.PostResource;
import cn.stylefeng.guns.modular.time.entity.Bookmarks;
import cn.stylefeng.guns.modular.time.model.request.BookmarksRequest;
import cn.stylefeng.guns.modular.time.service.BookmarksService;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 收藏网站控制器
 *
 * @author 叶新华
 * @date 2025/04/18 11:05
 */
@RestController
@ApiResource(name = "收藏网站")
public class BookmarksController {

    @Resource
    private BookmarksService bookmarksService;

    /**
     * 获取收藏分页
     *
     * @author 叶新华
     * @date 2025/04/18 11:05
     */
    @GetResource(name = "分页查询-收藏网站", path = "/bookmarks/page")
    public ResponseData<PageResult<Bookmarks>> page(
            @RequestParam(required = false) String searchText,
            @RequestParam(required = false, defaultValue = "1") Integer pageNo,
            @RequestParam(required = false, defaultValue = "20") Integer pageSize,
            @RequestParam(required = false) String sortField,
            @RequestParam(required = false) String sortOrder) {
        BookmarksRequest bookmarksRequest = new BookmarksRequest();
        bookmarksRequest.setSearchText(searchText);
        bookmarksRequest.setPageNo(pageNo);
        bookmarksRequest.setPageSize(pageSize);
        bookmarksRequest.setSortField(sortField);
        bookmarksRequest.setSortOrder(sortOrder);
        return new SuccessResponseData<>(bookmarksService.findPage(bookmarksRequest));
    }

    /**
     * 添加收藏
     *
     * @author 叶新华
     * @date 2025/04/18 11:05
     */
    @PostResource(name = "添加收藏", path = "/bookmarks/add", requiredPermission = true,
            requirePermissionCode = "bookmarks:add")
    @BizLog(logTypeCode = "bookmarks:add")
    public ResponseData<Bookmarks> add(@RequestBody @Validated(BookmarksRequest.add.class) BookmarksRequest bookmarksRequest) {
        bookmarksService.add(bookmarksRequest);
        return new SuccessResponseData<>();
    }

    /**
     * 删除收藏
     *
     * @author 叶新华
     * @date 2025/04/18 11:05
     */
    @PostResource(name = "删除收藏", path = "/bookmarks/delete", requiredPermission = true,
            requirePermissionCode = "bookmarks:delete")
    @BizLog(logTypeCode = "bookmarks:delete")
    public ResponseData<?> delete(@RequestBody @Validated(BookmarksRequest.delete.class) BookmarksRequest bookmarksRequest) {
        bookmarksService.del(bookmarksRequest);
        return new SuccessResponseData<>();
    }

    /**
     * 批量删除收藏
     *
     * @author 叶新华
     * @date 2025/04/18 11:05
     */
    @PostResource(name = "批量删除收藏", path = "/bookmarks/batchDelete", requiredPermission = true,
            requirePermissionCode = "bookmarks:delete")
    @BizLog(logTypeCode = "bookmarks:delete")
    public ResponseData<?> batchDelete(@RequestBody @Validated(BaseRequest.batchDelete.class) BookmarksRequest bookmarksRequest) {
        bookmarksService.batchDel(bookmarksRequest);
        return new SuccessResponseData<>();
    }

    /**
     * 编辑收藏
     *
     * @author 叶新华
     * @date 2025/04/18 11:05
     */
    @PostResource(name = "编辑收藏", path = "/bookmarks/edit", requiredPermission = true,
            requirePermissionCode = "bookmarks:edit")
    @BizLog(logTypeCode = "bookmarks:edit")
    public ResponseData<?> edit(@RequestBody @Validated(BookmarksRequest.edit.class) BookmarksRequest bookmarksRequest) {
        bookmarksService.edit(bookmarksRequest);
        return new SuccessResponseData<>();
    }

    /**
     * 查看收藏详情
     *
     * @author 叶新华
     * @date 2025/04/18 11:05
     */
    @GetResource(name = "查看收藏详情", path = "/bookmarks/detail")
    public ResponseData<Bookmarks> detail(@Validated(BookmarksRequest.detail.class) BookmarksRequest bookmarksRequest) {
        return new SuccessResponseData<>(bookmarksService.detail(bookmarksRequest));
    }

    /**
     * 更新点击次数
     *
     * @author 叶新华
     * @date 2025/04/18 11:20
     */
    @PostResource(name = "更新点击次数", path = "/bookmarks/click")
    public ResponseData<Void> updateClickCount(@RequestBody BookmarksRequest bookmarksRequest) {
        bookmarksService.updateClickCount(bookmarksRequest);
        return new SuccessResponseData<>();
    }

    /**
     * 更新书签置顶状态
     *
     * @author xixiaowei
     * @date 2024/03/21 10:30:00
     */
    @PostResource(name = "更新书签置顶状态", path = "/bookmarks/pinned", requiredPermission = true,requirePermissionCode = "bookmarks:pinned")
    public ResponseData<?> pinned(@RequestBody BookmarksRequest bookmarksRequest) {
        bookmarksService.edit(bookmarksRequest);
        return new SuccessResponseData<>();
    }
}
